$(function () {
    $("#jqGrid").Grid({
        url: '../mihealthdata/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true,width: 30},
			{label: '会员id', name: 'userId', index: 'user_id', width: 30},
			{label: '会员昵称', name: 'nickName', index: 'nick_name', width: 80},
			{label: '身体得分', name: 'score', index: 'score', width: 80},
			{label: '称重时间', name: 'createDate', index: 'create_date', width: 80},
			{label: '体重', name: 'weight', index: 'weight', width: 80},
			{label: '体重描述', name: 'weightDesc', index: 'weight_desc', width: 80},
			{label: '体型指标', name: 'weightTarget', index: 'weight_target', width: 80},
			{label: 'BMI', name: 'bmi', index: 'bmi', width: 80},
			{label: '体脂', name: 'bodyfat', index: 'bodyfat', width: 80},
			{label: '体脂达标情况', name: 'bodyfatExplain', index: 'bodyfat_explain', width: 80},
			{label: '基础代谢（千卡）', name: 'basalMetabolism', index: 'basal_metabolism', width: 80},
			{label: '肌肉', name: 'muscle', index: 'muscle', width: 80},
			{label: '内脏脂肪', name: 'visceralFat', index: 'visceral_fat', width: 80},
            {
                label: '操作', width: 160, align: 'center', sortable: false, formatter: function (value, col, row) {
                    return '<button class="btn btn-outline btn-info" onclick="vm.updateById(' + row.id + ')"><i class="fa fa-info-circle"></i>&nbsp;修改</button>' +
                        '<button class="btn btn-outline btn-primary" onclick="vm.deleteById(' + row.id + ')"><i class="fa fa-print"></i>&nbsp;删除</button>';
                }
            }
		]
    });
    vm.initUserList();
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
        userList:[],
		miHealthData: {userId:null},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
			userId: null
		},
		miPic:null,
		UploadMiPicUrl:"../mihealthdata/upload/"
	},
	methods: {
		initUserList:function(){
            Ajax.request({
			    url: "../user/queryAll",
                type: "POST",
                successCallback: function (r) {
            		vm.userList = r.list;
				}
			});
		},
		selectUser:function(userId){
			vm.q.userId = userId;
		},
		selectUserEdit:function(userId){
			vm.miHealthData.userId = userId;
			vm.UploadMiPicUrl = "../mihealthdata/upload/" + userId;
		},
		uploadSelectUser:function(userId){
			vm.miHealthData.userId = userId;
		},
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.UploadMiPicUrl = "../mihealthdata/upload/";
			vm.miHealthData = {userId:null};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		updateById: function (id) {
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.miHealthData.id == null ? "../mihealthdata/save" : "../mihealthdata/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.miHealthData),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../mihealthdata/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		deleteById: function (id) {
			confirm('确定要删除？', function () {
                Ajax.request({
				    url: "../mihealthdata/delete",
                    params: JSON.stringify([id]),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../mihealthdata/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.miHealthData = r.miHealthData;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'userId': vm.q.userId},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
    			userId: null
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccessPic:function(data){
        	//alert("上传成功！"+JSON.stringify(data));
        	if(data.code == 0){
        		alert("上传成功！");
        	}else{
        		alert("上传失败:"+data.msg);
        	}
        	vm.reload();
        }
	}
});